package com.thinkfit.jy.activity;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.common.lib.activity.BaseActivity;
import com.common.lib.constant.Constants;
import com.common.lib.constant.EventBusEvent;
import com.common.lib.fragment.BaseFragment;
import com.common.lib.manager.DataManager;
import com.common.lib.mvp.contract.EmptyContract;
import com.common.lib.mvp.presenter.EmptyPresenter;
import com.thinkfit.jy.R;
import com.thinkfit.jy.dialog.CustomSportsPopupWindow;
import com.thinkfit.jy.fragment.SetTargetFragment;
import com.thinkfit.jy.serialport.SerialPortManager;
import com.thinkfit.jy.utils.BluetoothOrder;
import com.thinkfit.jy.utils.CountDownViewUtils;
import com.thinkfit.jy.utils.DeviceTypeUtils;
import com.thinkfit.jy.utils.DialogViewUtils;
import com.thinkfit.jy.utils.SimpleSettingViewUtils;
import com.thinkfit.jy.utils.StringUtils;
import com.thinkfit.jy.utils.ThemeUtils;
import com.thinkfit.jy.utils.Utils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

public class SetTargetActivity extends BaseActivity<EmptyContract.Presenter> implements EmptyContract.View {

    private ArrayList<BaseFragment> mFragments;
    private int mCurrentItem;
    private int mMainStyle;
    TextView tvItem1,tvItem2,tvItem3,tvItem4,tvItem5,tvItem6,tvItem7,tvItem8,tvArea,tvLanguage,tvUnit;
    ConstraintLayout clMain;
    ArrayList<String> distanceList = new ArrayList<>();
    ArrayList<String> kcalList = new ArrayList<>();
    ArrayList<String> timeList = new ArrayList<>();
    ArrayList<TextView> tvList = new ArrayList<>();
    @Override
    protected int getLayoutId() {
        return R.layout.activity_set_target;
    }

    @Override
    protected void onCreated(@Nullable Bundle savedInstanceState) {

        setViewsOnClickListener(R.id.tv0, R.id.tv1, R.id.tv2,R.id.ll_bcak,R.id.cl_set_item);
        tvList.clear();

        clMain = findViewById(R.id.cl_main);
        tvItem1 = findViewById(R.id.tv_item1);
        tvItem2 = findViewById(R.id.tv_item2);
        tvItem3 = findViewById(R.id.tv_item3);
        tvItem4 = findViewById(R.id.tv_item4);
        tvItem5 = findViewById(R.id.tv_item5);
        tvItem6 = findViewById(R.id.tv_item6);
        tvItem7 = findViewById(R.id.tv_item7);
        tvItem8 = findViewById(R.id.tv_item8);

        tvArea = findViewById(R.id.tv_area);
        tvLanguage = findViewById(R.id.tv_language);
        tvUnit = findViewById(R.id.tv_unit);

        tvList.add(tvItem1);
        tvList.add(tvItem2);
        tvList.add(tvItem3);
        tvList.add(tvItem4);
        tvList.add(tvItem5);
        tvList.add(tvItem6);
        tvList.add(tvItem7);
        initDistanceList();
//        initKcalList();
//        initTimeList();


        mFragments = new ArrayList<>();
        mFragments.add(SetTargetFragment.getInstance(0));//设置目标几分钟
        mFragments.add(SetTargetFragment.getInstance(1));//设置目标几公里
        mFragments.add(SetTargetFragment.getInstance(2));//设置目标多少卡路里
        ViewPager viewPager = findViewById(R.id.viewPager);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }

        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mCurrentItem = position;
                resetBtn();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mCurrentItem = getIntent().getIntExtra(Constants.BUNDLE_EXTRA, 0);
        viewPager.setCurrentItem(mCurrentItem);
        tvItem1.setOnClickListener(this);
        tvItem2.setOnClickListener(this);
        tvItem3.setOnClickListener(this);
        tvItem4.setOnClickListener(this);
        tvItem5.setOnClickListener(this);
        tvItem6.setOnClickListener(this);
        tvItem7.setOnClickListener(this);
        tvItem8.setOnClickListener(this);

        mCurrentItem = 0;
        resetBtn();
        Utils.initRegion(tvArea,tvLanguage,tvUnit);
    }

    @Override
    protected void onResume() {
        super.onResume();
        clMain.setBackgroundResource(ThemeUtils.getInstance().getMainTheme());
        initSound();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceive(@NonNull HashMap<String, Object> map) {
        if (map == null) {
            return;
        }
        if(map.containsKey(EventBusEvent.TO_VIDEO)){
//            Bundle bundle = new Bundle();
//            bundle.putSerializable(Constants.BUNDLE_EXTRA, mVideo);
            openActivity(FreeMovementNewActivity.class);
        }else if(map.containsKey(EventBusEvent.VOICE_CHANGE)){
            int i = (int) map.get(EventBusEvent.VOICE_CHANGE);
            if(i <= 0){
                setImage(R.id.iv_sound,R.drawable.img_simple_set_sound_off);

            }else {
                setImage(R.id.iv_sound,R.drawable.img_simple_set_sound_max);
            }
//            SportsStatusBarViewUtils.getInstance().setVolume((int) map.get(EventBusEvent.VOICE_CHANGE));
        }
    }

    @NonNull
    @Override
    protected EmptyContract.Presenter onCreatePresenter() {
        return new EmptyPresenter(this);
    }

    ArrayList<Float> setTarget = new ArrayList<>();
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cl_set_item:
                SimpleSettingViewUtils.getInstance().initView(getContext(),this);
                break;
            case R.id.ll_bcak:
                finish();
                break;
            case R.id.tv0:
                mCurrentItem = 0;
                resetBtn();
                refreshAnimation(1);
//                ViewPager viewPager = findViewById(R.id.viewPager);
//                viewPager.setCurrentItem(0);
                break;
            case R.id.tv1:
                mCurrentItem = 1;
                resetBtn();
                refreshAnimation(2);
//                viewPager = findViewById(R.id.viewPager);
//                viewPager.setCurrentItem(1);
                break;
            case R.id.tv2:
                mCurrentItem = 2;
                resetBtn();
                refreshAnimation(3);
//                viewPager = findViewById(R.id.viewPager);
//                viewPager.setCurrentItem(2);
                break;
            case R.id.tv_item1:
                initTvItem(tvItem1.getText().toString());
                break;
            case R.id.tv_item2:
                initTvItem(tvItem2.getText().toString());
                break;
            case R.id.tv_item3:
                initTvItem(tvItem3.getText().toString());
                break;
            case R.id.tv_item4:
                initTvItem(tvItem4.getText().toString());
                break;
            case R.id.tv_item5:
                initTvItem(tvItem5.getText().toString());
                break;
            case R.id.tv_item6:
                initTvItem(tvItem6.getText().toString());
                break;
            case R.id.tv_item7:
                initTvItem(tvItem7.getText().toString());
                break;
            case R.id.tv_item8:
                CustomSportsPopupWindow customSportsPopupWindow = new CustomSportsPopupWindow(getContext(), mCurrentItem + 1);
                customSportsPopupWindow.show(this);
                break;

        }
    }

    public void initTvItem(String item){
        setTarget.clear();
        setTarget.add((mCurrentItem+1f));
        setTarget.add(Float.valueOf(item));
        DataManager.Companion.getInstance().saveSetTarget(setTarget);

        if(DataManager.Companion.getInstance().isLogin()){
            if(DeviceTypeUtils.getDeviceType() == 2){
                if(DeviceTypeUtils.connectDeviceType() == 0){
                    if(DeviceTypeUtils.getInstance().isTreadmill()){
                        BluetoothOrder.getInstance().startTreadmill();
                    }else {
                        BluetoothOrder.getInstance().startPlay();
                    }
                }else {
                    SerialPortManager.getInstance().startRun();
                }
            }else {
                CountDownViewUtils.getInstance().initView(getContext(),3);
            }
//                    CountDownViewUtils.getInstance().initView(mContext,3);
        }else {
            DialogViewUtils.getInstance().initView(getContext(),false,DialogViewUtils.TOURIST_PATTERN);
        }
    }

    Collection<Animator> animatorList = new ArrayList<>();
    public void refreshAnimation(int type){
        animatorList.clear();
        for (int i = 0;i<tvList.size();i++){
            ObjectAnimator animator = ObjectAnimator.ofFloat(tvList.get(i), "alpha", 1.0f, 0.0f);
            animator.setDuration(500);
            animatorList.add(animator);
        }
        ObjectAnimator animator1= ObjectAnimator.ofFloat(tvItem8, "alpha", 1.0f, 0.0f);
        animator1.setDuration(500);
        animatorList.add(animator1);
        AnimatorSet buttonAnimatorSet = new AnimatorSet();
        buttonAnimatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if(type == 2){
                    initTimeList();
                }else if(type == 3){
                    initKcalList();
                }else {
                    initDistanceList();
                }
                buttonAnimatorSet.cancel();
                showAnimation();
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                buttonAnimatorSet.cancel();
            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        buttonAnimatorSet.playTogether(animatorList);//.after(rotationXAnimator)
        buttonAnimatorSet.start();

    }
    public void showAnimation(){
        animatorList.clear();
        for (int i = 0;i<tvList.size();i++){
            ObjectAnimator animator = ObjectAnimator.ofFloat(tvList.get(i), "alpha", 0.0f, 1.0f);
            animator.setDuration(500);
            animatorList.add(animator);
        }
        ObjectAnimator animator1= ObjectAnimator.ofFloat(tvItem8, "alpha", 0.0f, 1.0f);
        animator1.setDuration(500);
        animatorList.add(animator1);
        AnimatorSet buttonAnimatorSet = new AnimatorSet();
        buttonAnimatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                buttonAnimatorSet.cancel();
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                buttonAnimatorSet.cancel();
            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        buttonAnimatorSet.playTogether(animatorList);//.after(rotationXAnimator)
        buttonAnimatorSet.start();

    }



    public void initDistanceList(){
        distanceList.clear();
        distanceList.add("3");
        distanceList.add("6");
        distanceList.add("9");
        distanceList.add("12");
        distanceList.add("10");
        distanceList.add("21.098");
        distanceList.add("42.195");
        for (int i = 0; i < tvList.size(); i++){
            tvList.get(i).setText(distanceList.get(i));
        }

    }
    public void initKcalList(){
        kcalList.clear();
        kcalList.add("100");
        kcalList.add("200");
        kcalList.add("300");
        kcalList.add("400");
        kcalList.add("500");
        kcalList.add("1000");
        kcalList.add("1500");
        for (int i = 0; i < tvList.size(); i++){
            tvList.get(i).setText(kcalList.get(i));
        }
    }
    public void initTimeList(){
        timeList.clear();
        timeList.add("15");
        timeList.add("30");
        timeList.add("45");
        timeList.add("60");
        timeList.add("90");
        timeList.add("120");
        timeList.add("150");
        for (int i = 0; i < tvList.size(); i++){
            tvList.get(i).setText(timeList.get(i));
        }
    }

    private void resetBtn() {
        for (int i = 0; i < 3; ++i) {
            int id = getResources().getIdentifier("tv" + i, "id", getPackageName());
            TextView tv = findViewById(id);
            if (i == mCurrentItem) {
                tv.setBackgroundResource(R.drawable.shape_gradient_d0b085_a87c42_270_32);
                tv.setTypeface(null, Typeface.BOLD);
                tv.setTextColor(ContextCompat.getColor(this, com.common.lib.R.color.white));
            } else {
                tv.setBackground(null);
                tv.setTypeface(null, Typeface.NORMAL);
                tv.setTextColor(ContextCompat.getColor(this, com.common.lib.R.color.color_60_ff_ff_ff));
            }
        }
    }

    public void initSound(){
        AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        if(currentVolume <= 0){
            setImage(R.id.iv_sound,R.drawable.img_simple_set_sound_off);

        }else {
            setImage(R.id.iv_sound,R.drawable.img_simple_set_sound_max);
        }
    }
}